package com.qf.web.servlet;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@WebServlet("/test")
public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取 上下文（全局）对象
        ServletContext servletContext = req.getServletContext();
        //动态获取服务器部署的真实路径
        String realPath = servletContext.getRealPath("/");
        System.out.println(realPath);

        Object countObj = servletContext.getAttribute("count");
        if(countObj == null) {
            servletContext.setAttribute("count", 1);
        }else {
            servletContext.setAttribute("count", (int)countObj + 1);
        }

        System.out.println("第" + servletContext.getAttribute("count") + "此访问");

//        ServletContext servletContext1 = req.getSession().getServletContext();
//        System.out.println(servletContext1);
    }


}
